package com.fengzai.cglibproxy;

import net.sf.cglib.proxy.*;

import java.lang.reflect.Method;


/**
 * Created by  峰仔  on 2017-08-03
 */
public class Zhongjie implements MethodInterceptor {
    Fangdong target;

    public Object getInstance(Fangdong fangdong) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(fangdong.getClass());
        enhancer.setCallback(this);
        target = (Fangdong) enhancer.create();
        return target;
    }

    /**
     * d第二种
     * @return
     */
    public Object getInstance2(String s) {
        Class<?> aClass = null;
        try {
            aClass = Class.forName(s);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(aClass);
        enhancer.setCallback(this);
        target = (Fangdong) enhancer.create();
        return target;
    }
    /**
     * d第二种
     * @return
     */
    public Object getInstance3( Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        target = (Fangdong) enhancer.create();
        return target;
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println(method);
        System.out.println("中介进行发布消息");
        Object invoke = methodProxy.invokeSuper(target, objects);
        System.out.println("中介进行善后工作");
        return invoke;
    }
}
